package org.ithang.tools.timer;

import java.util.List;

import org.ithang.application.account.bean.Account;
import org.ithang.application.account.service.AccountService;
import org.ithang.application.customer.bean.Customer;
import org.ithang.application.customer.service.CustomerService;
import org.ithang.application.invest.bean.Invest;
import org.ithang.application.invest.service.InvestService;
import org.ithang.application.staticBonus.bean.StaticBonus;
import org.ithang.application.staticBonus.service.StaticBonusService;
import org.ithang.system.keyvalue.bean.Keyvalue;
import org.ithang.system.keyvalue.service.KeyvalueService;
import org.ithang.tools.lang.DateUtils;
import org.ithang.tools.model.Arith;
import org.ithang.tools.redis.Keys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;


/**
 * 任务Handler示例（Bean模式）
 *
 * 开发步骤：
 * 1、继承"IJobHandler"：“com.xxl.job.core.handler.IJobHandler”；
 * 2、注册到Spring容器：添加“@Component”注解，被Spring容器扫描为Bean实例；
 * 3、注册到执行器工厂：添加“@JobHandler(value="自定义jobhandler名称")”注解，注解value值对应的是调度中心新建任务的JobHandler属性的值。
 * 4、执行日志：需要通过 "XxlJobLogger.log" 打印执行日志；
 *
 * 静态分润
 */
@JobHandler(value="staticBonusJobHandler")
@Component
public class StaticBonusJobHandler extends IJobHandler {

	@Autowired
	private CustomerService customerService;
	
	@Autowired
    private KeyvalueService KeyvalueService;
	
	@Autowired
	private InvestService investService;
	
	@Autowired
	private StaticBonusService staticBonusService;
	
	@Autowired
	private AccountService accountService;
	
	@Override
	public ReturnT<String> execute(String param) throws Exception {
		XxlJobLogger.log("开始执行静态分润");
		Keyvalue isStaticBonus=KeyvalueService.get(Keys.IsStaticBonus);//是否静态分润
		Keyvalue staticBonusRate=KeyvalueService.get(Keys.StaticBonusRate);//静态分润率
		
		if(1==isStaticBonus.intValue()&&staticBonusRate.intValue()>0){
			
			List<Account> accounts=accountService.list();
			for(Account account:accounts){
				//计算总投资 静态分润后的收益
				/*double bonus=Arith.mul(account.getInvest_amount(),(Arith.div(staticBonusRate.intValue(),100D,2)));//利息
				if((bonus+account.getStatic_bonus()+account.getTeam_bonus())>account.getInvest_amount()){
					bonus=account.getInvest_amount()-(account.getStatic_bonus()+account.getTeam_bonus());
					
					StaticBonus staticBonus=new StaticBonus();
					staticBonus.setAmount(bonus);
					staticBonus.setCustomer_id(account.getCustomer_id());
					staticBonus.setInvest_id(0);//最终分润
					staticBonus.setCreate_time(DateUtils.getSystime());
					
					staticBonusService.add(staticBonus);//添加分润记录
					
					Account upact=new Account();
					upact.setId(account.getId());
					upact.setAmount(Arith.add(account.getAmount(),bonus));
					upact.setStatic_bonus(Arith.add(account.getStatic_bonus(),bonus));
					accountService.update(upact);//更新账户信息
				}*/
				
				List<Invest> invests=investService.listByAccount(account.getId());
				if(null!=invests&&!invests.isEmpty()){
					for(Invest invest:invests){
						double bonus=Arith.mul(invest.getAmount(),(Arith.div(staticBonusRate.intValue(),100D,5)));//利息
						
						//如果分润可能超出上线，则 用 上线-当前分润=本次分润
						if((bonus+account.getStatic_bonus()+account.getTeam_bonus())>account.getInvest_amount()){
							bonus=account.getInvest_amount()-(account.getStatic_bonus()+account.getTeam_bonus());
						}
							
						StaticBonus staticBonus=new StaticBonus();
						staticBonus.setAmount(bonus);
						staticBonus.setCustomer_id(account.getCustomer_id());
						staticBonus.setInvest_id(invest.getId());//最终分润
						staticBonus.setCreate_time(DateUtils.getSystime());
						
						staticBonusService.add(staticBonus);//添加分润记录
						
						account.setAmount(Arith.add(account.getAmount(),bonus));
						account.setStatic_bonus(Arith.add(account.getStatic_bonus(),bonus));
						accountService.update(account);//更新账户信息	
						
					}
				}
				
			}
			
			
			
			/*List<Invest> invests=investService.list();//查询所有投资记录
			for(Invest invest:invests){
				
				if(invest.getProfit()<invest.getAmount()){
					double ge=Arith.mul(invest.getAmount(),(Arith.div(staticBonusRate.intValue(),100D,2)));//利息
					StaticBonus staticBonus=new StaticBonus();
					staticBonus.setAmount(ge);
					staticBonus.setCustomer_id(invest.getCustomer_id());
					staticBonus.setInvest_id(invest.getId());
					staticBonus.setCreate_time(DateUtils.getSystime());
					
					staticBonusService.add(staticBonus);//添加分润记录
					Account act=accountService.getByCustomer(invest.getCustomer_id());//得到账户信息
					act.setAmount(Arith.add(act.getAmount(),ge));
					act.setStatic_bonus(Arith.add(act.getStatic_bonus(),ge));
					
					Account upact=new Account();
					upact.setId(act.getId());
					upact.setAmount(act.getAmount());
					upact.setStatic_bonus(act.getStatic_bonus());
					upact.setInvest_amount(null);
					upact.setTeam_invest_amount(null);
					accountService.update(upact);//更新账户信息
				}
			}*/
		}
		
		XxlJobLogger.log("结束执行静态分润");
		return SUCCESS;
	}

}
